package com.fingard.dsp.bank.directbank.vbao03;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.vbao03.request.ContractApplyForm;
import com.fingard.dsp.bank.directbank.vbao03.trade.VbaoChargeSystemHandler;
import com.vbao.frame.common.util.Base64;
import com.vbao.frame.common.util.RsaUtil;
import com.vbao.frame.common.util.SignatureUtils;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;

public class VBAO03Base extends DirectBase{
	public String getCharset() {
		return getBankConfig().getCharset("UTF-8");
	}
	
	public String getSFTPDir(String p_reqPath) {
		if (p_reqPath.endsWith("/")) {
			return p_reqPath;
		} else {
			return p_reqPath + "/";
		}
	}
	
	/**
	 * 数据发送银行
	 * 
	 * @param reqData
	 * @param actItem
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public JSONObject sendToBank(String reqData, ActSetItem actItem, String url) throws Exception {
		// 1、MD5签名
		@SuppressWarnings("unchecked")
		Map<String, Object> mapSend = JSONObject.parseObject(reqData, Map.class);
        WriteBankLogLn2("微保签名原始数据:\r\n" + reqData);
        String sign = SignatureUtils.getSignThrowsJsonString(reqData);
        mapSend.put("sign", sign);
        
        reqData = JSONObject.toJSONString(mapSend);
        WriteBankLogLn2("发送微保请求数据明文:\r\n" + reqData);
        
        // 2、RSA公钥加密
        PublicKey publicKey = RsaUtil.getRSAPublicKeyByFileSuffix(actItem.oppCertFilePath, "pem", "RSA");
        byte[] encryBytes = RsaUtil.encrypt(reqData.getBytes(getCharset()), publicKey, 1024, 11, "RSA/ECB/PKCS1Padding");
        reqData = new String(Base64.encode(encryBytes), getCharset());
        WriteBankLogLn2("发送微保请求数据密文:\r\n" + reqData);

		ContractApplyForm applyForm = new ContractApplyForm();
		applyForm.setAppId(actItem.appId);
		applyForm.setData(reqData);
		JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(applyForm);
		reqData = JSON.toJSONString(jsonObject1);
        WriteBankUrlLogLn(url);

		VbaoChargeSystemHandler vbaoChargeHandler = new VbaoChargeSystemHandler();
		String tmpStrRet = vbaoChargeHandler.httpjson(url, reqData, getCharset());
        WriteBankLogLn2("微保响应数据密文:\r\n" + tmpStrRet);
        
		return JSONObject.parseObject(tmpStrRet);
	}
	
	/**
	 * RSA解密
	 * 
	 * @param p_reqJson
	 * @param actItem
	 * @return
	 * @throws Exception
	 */
	public JSONObject decryptByRSA(JSONObject p_reqJson, ActSetItem actItem) throws Exception {
		PrivateKey privateKey = RsaUtil.getRSAPrivateKeyByFileSuffix(actItem.ownKeyStorePath, "pem", null, "RSA");
        byte[] decodeBase64Bytes = Base64.decode(p_reqJson.getString("data"));
        String decodeStr = new String(RsaUtil.decrypt(decodeBase64Bytes, privateKey, 1024	, 11, "RSA/ECB/PKCS1Padding"),getCharset());
        WriteBankLogLn2("解密微保响应数据明文:\r\n" + decodeStr);
        
        return JSONObject.parseObject(decodeStr);
	}
}
